package cn.tangsihai.pet.controller;

import cn.tangsihai.basic.exception.MyException;
import cn.tangsihai.basic.util.AjaxResult;
import cn.tangsihai.basic.util.LoginContextUtil;
import cn.tangsihai.basic.util.PageList;
import cn.tangsihai.pet.domain.Pet;
import cn.tangsihai.pet.domain.SearchMasterMsg;
import cn.tangsihai.pet.query.SearchMasterMsgQuery;
import cn.tangsihai.pet.service.ISearchMasterMsgService;
import cn.tangsihai.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;
    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return seachMasterMsgService.findAll();
    }

    /**
     * 发布寻主
     * @param smm
     * @param request
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg smm, HttpServletRequest request){
        try {
            // 通过请求头获取用户登录信息
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);
            seachMasterMsgService.publish(smm, logininfo.getId());
            return AjaxResult.me();
        }catch (MyException e) {
            return AjaxResult.me().setMsg(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统异常");
        }
    }

    /**
     * 待处理：
     *  由平台工作人员使用，即根据登录信息查询employee表中有shop_id的用户
     * @return
     */
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery msgQuery, HttpServletRequest request){
        try {
            // 通过请求头获取用户登录信息
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);
            return seachMasterMsgService.pending(msgQuery, logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new PageList<>();
    }

    /**
     * 已处理
     *  由所有人员使用，即根据登录信息查询employee表中没有shop_id的用户
     * @return
     */
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery msgQuery, HttpServletRequest request){
        try {
            // 通过请求头获取用户登录信息
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);
            return seachMasterMsgService.processed(msgQuery, logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new PageList<>();
    }

    /**
     * 处理待处理宠物接口
     * @return
     */
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);
            seachMasterMsgService.handle(pet, logininfo.getId());
            return AjaxResult.me();
        }catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统繁忙");
        }
    }

}
